package com.quectel.core.module.securitypatrol.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.securitypatrol.dao.SecurityPatrolRouteDao;
import com.quectel.core.module.securitypatrol.dto.SecurityPatrolRouteDto;
import com.quectel.core.module.securitypatrol.entity.SecurityPatrolRouteEntity;
import com.quectel.core.module.securitypatrol.service.SecurityPatrolRouteService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.common.JacksonUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/05/07 14:10
 */
@DubboService
public class SecurityPatrolRouteServiceImpl implements SecurityPatrolRouteService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SecurityPatrolRouteServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SECURITY_PATROL_ROUTE_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SECURITY_PATROL_ROUTE_CACHE_BY_ID:%d", id);

    @Autowired
    private SecurityPatrolRouteDao securityPatrolRouteDao;
    @Autowired
    private VillageService villageService;
    @Autowired
    private DeviceService deviceService;

    @Override
    public SecurityPatrolRouteDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SECURITY_PATROL_ROUTE_CACHE_BY_ID_FUNC.apply(id),

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SecurityPatrolRouteDto selectById(Long id) {
        SecurityPatrolRouteEntity entity = securityPatrolRouteDao.selectById(id);
        SecurityPatrolRouteDto securityPatrolRouteDto = paddingField(CopyUtils.copyObj(entity, SecurityPatrolRouteDto.class));
        return securityPatrolRouteDto;
    }

    @Override
    public List<SecurityPatrolRouteDto> queryList(Map<String, Object> params) {
        List<SecurityPatrolRouteEntity> list = securityPatrolRouteDao.queryList(params);
        List<SecurityPatrolRouteDto> result = CopyUtils.copyList(list, SecurityPatrolRouteDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private SecurityPatrolRouteDto paddingField(SecurityPatrolRouteDto dto) {
        if (dto != null) {
            if (dto.getVillageId() != null) {
                VillageDto villageDto = villageService.selectCacheById(dto.getVillageId());
                if (villageDto != null) {
                    dto.setVillageName(villageDto.getName());
                }
            }
            //补充园区信息
            dto.setVillageDto(villageService.selectById(dto.getVillageId()));
            //补充设备信息
            if (StringUtils.isNotBlank(dto.getRouteArr())) {
                List<Long> deviceIds = JacksonUtils.parseArray(dto.getRouteArr(), Long.class);
                List<DeviceDto> deviceDtoList = new ArrayList<>();
                for (Long deviceId : deviceIds) {
                    DeviceDto deviceDto = deviceService.selectById(deviceId);
                    if (deviceDto != null) {
                        deviceDtoList.add(deviceDto);
                    }
                    if (deviceDtoList.size() > 0) {
                        dto.setDeviceDtoList(deviceDtoList);
                    }
                }
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return securityPatrolRouteDao.queryTotal(params);
    }

    @Override
    public Long save(SecurityPatrolRouteDto dto) {
        SecurityPatrolRouteEntity entity = CopyUtils.copyObj(dto, SecurityPatrolRouteEntity.class);
        entity.setId(Snowflake.nextId());
        securityPatrolRouteDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SecurityPatrolRouteDto dto) {
        SecurityPatrolRouteEntity entity = CopyUtils.copyObj(dto, SecurityPatrolRouteEntity.class);
        securityPatrolRouteDao.updateById(entity);
        CacheKit.invalidRedisCache(SECURITY_PATROL_ROUTE_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void updateAllColumnById(SecurityPatrolRouteDto dto) {
        SecurityPatrolRouteEntity entity = CopyUtils.copyObj(dto, SecurityPatrolRouteEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, securityPatrolRouteDao);

        CacheKit.invalidRedisCache(SECURITY_PATROL_ROUTE_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void deleteById(Long id) {
        securityPatrolRouteDao.deleteById(id);

        CacheKit.invalidRedisCache(SECURITY_PATROL_ROUTE_CACHE_BY_ID_FUNC.apply(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
